/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.wii.elpro.web.action;

import com.wii.elpro.entity.Quotation;
import com.wii.elpro.entity.QuotationItem;
import com.wii.elpro.entity.User;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;

/**
 *
 * @author hp02
 */
public class QuotationNewRemoveItemAction extends Action {
    @Override
    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response) throws Exception {
        HttpSession sess = request.getSession(true);
        User user = (User)sess.getAttribute("userSession");       
        if(user==null){
            return mapping.findForward("gagalLogin");            
        }  
        ActionForward af;
        int index = Integer.parseInt(request.getParameter("id"));        
        List<QuotationItem> items;
        ActionMessages messages = new ActionMessages();
        Quotation quotation = null;
        if(sess.getAttribute("quotation")!=null) {
            quotation = (Quotation) sess.getAttribute("quotation");
        }
        try {
            if(sess.getAttribute("listItem")!=null) {
                items = (List<QuotationItem>) sess.getAttribute("listItem");
                items.remove(index);
                messages.add("customMessage", new ActionMessage("customMessage", "Material is successfully deleted from product (session state)."));
                saveMessages(sess, messages);
            } else {
                items = new ArrayList<QuotationItem>();
                messages.add("customMessage", new ActionMessage("customMessage", "There is no material to delete from session."));
                saveMessages(sess, messages);
            }
            af = new ActionForward(mapping.findForward("generate").getPath()+"?step=generate&id="+quotation.getSpdcf().getId(), true);
        } catch (Exception ex) {
            messages.add("customMessage", new ActionMessage("customMessage", "Application is fail to remove material from product (session state). Please contact my developer."));
            saveMessages(sess, messages);
            ex.printStackTrace();
            af = new ActionForward(mapping.findForward("generate").getPath()+"?step=generate&id="+quotation.getSpdcf().getId(), true);
        }
        return af;
    }
}
